package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.base.utils.CacheUtil;
import cn.tedu._03vehicle.mapper.NoticeMapper;
import cn.tedu._03vehicle.pojo.entity.Notice;
import cn.tedu._03vehicle.pojo.vo.NoticeVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Api(tags = "03.公告管理")
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @Autowired
    private CacheUtil cacheUtil;
    @GetMapping("list")
    @ApiOperation("公告首页展示")
    @ApiOperationSupport(order = 10)
    public JsonResult noticelist(){
        log.debug("notice = " );
        //先走redis 必须先弄get命令
        log.debug("首页列表展示");
        Serializable noticeIndex = cacheUtil.get("notice_index");
        if (noticeIndex != null){
            log.debug("首页数据来自redis数据库。。。");
            return JsonResult.ok(noticeIndex);
//        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
//        Serializable noticeIndex = ops.get("notice_index");
//        if (noticeIndex != null){
//            log.debug("首页数据来自redis数据库。。。");
//            return JsonResult.ok(noticeIndex);
        }
        //缓存中不存在数据
        log.debug("首页数据来自MySQL数据库。。。");
        List<NoticeVO> noticeList = noticeMapper.selectByQuery();
        if(noticeList == null){
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
        cacheUtil.setExpire("notice_index",(Serializable) noticeList,30);
        return JsonResult.ok(noticeList);
    }
}
